# coding=UTF-8
import os
import time

import requests
from utils import json_util, k_util, doc_util

base_path = os.getcwd().split('\\StockMonitor')[0] + '\\StockMonitor\\'
stock_path = base_path.split('\\stock')[0] + '\\stock\\'

# 板块市值前五
utl1 = 'http://push2.eastmoney.com/api/qt/clist/get?pi=0&pz=6&po=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fs=b:BK0433&fid=f20&fields=f1,f12,f13,f14,f152,f20&cb=jQuery11240525843976698429_1608625839397&_=1608625839398'
# 净利润前五
url2 = 'http://push2.eastmoney.com/api/qt/clist/get?pi=0&pz=6&po=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fs=b:BK0433&fid=f45&fields=f1,f12,f13,f14,f152,f45&cb=jQuery11240525843976698429_1608625839399&_=1608625839400'
# 市盈率前五
url3 = 'http://push2.eastmoney.com/api/qt/clist/get?pi=0&pz=6&po=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fs=b:BK0433&fid=f9&fields=f1,f12,f13,f14,f152,f9&cb=jQuery11240525843976698429_1608625839401&_=1608625839402'
# 毛利率率前五
url4 = 'http://push2.eastmoney.com/api/qt/clist/get?pi=0&pz=6&po=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fs=b:BK0433&fid=f49&fields=f1,f12,f13,f14,f152,f49&cb=jQuery11240525843976698429_1608625839407&_=1608625839408'
# 获取东方财富所有的板块信息
url_all_plate_dfcf = 'http://quote.eastmoney.com/api/PageConfigs/sidemenus.json'


# 获取东方财富优质股
def get_plate_good_stock():
    good_stock_dict = {}
    resp1 = requests.get(url_all_plate_dfcf)
    resp1_content = str(resp1.content.decode('utf-8').replace('true', '1').replace('false', '0'))
    plate_list = eval(resp1_content)
    for plate_one in plate_list:
        key = plate_one['key']
        if key == 'hsbroad':
            next = plate_one['next']
            for plate_two in next:
                next_two = plate_two['next']
                key = plate_two['key']
                if key != 'industry_board':  # 行业板块
                    continue
                for plate_thr in next_two:
                    key_thr = plate_thr['key']
                    palte = key_thr.split('.')[1]
                    f_id_list = ['f20', 'f45', 'f9', 'f49']
                    for f_id in f_id_list:
                        time_stamp = int(round(time.time() * 1000))
                        utl_sz_five = 'http://push2.eastmoney.com/api/qt/clist/get?pi=0&pz=6&po=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fs=b:' + '11111' + '&fid=' + '44444' + '&fields=f1,f12,f13,f14,f152,f20&cb=jQuery11240525843976698429_' + '22222' + '&_=' + '33333'
                        utl_sz_five = utl_sz_five.replace('11111', palte)
                        utl_sz_five = utl_sz_five.replace('22222', str(time_stamp))
                        utl_sz_five = utl_sz_five.replace('33333', str(time_stamp + 1))
                        utl_sz_five = utl_sz_five.replace('44444', f_id)
                        resp = requests.get(utl_sz_five)
                        data = eval(resp.content.decode('utf-8').split('(')[1].split(')')[0])['data']['diff']
                        for key in data:
                            stock_info = data[key]
                            code = stock_info['f12']
                            if code[0:2] == '60' or code[0:2] == '00':
                                name = stock_info['f14']
                                stock_info = {'code': code, 'name': name, 'plate': palte}
                                if code not in good_stock_dict.keys():
                                    good_stock_dict[code] = stock_info
    good_stock_list = []
    for key in good_stock_dict:
        good_stock_list.append(good_stock_dict[key])
    doc_util.gen_new_doc(stock_path + '\\common\\stocks\\', '10.板块前五', good_stock_list)


# real-time 股票当前动态
def rt_dynamic(code):
    '''
    f2:最新价格(精确到分)
    f3:涨跌幅(*100)
    f12:代码
    f14:名称
    f62:主力净流入金额
    f66:超大单净流入金额
    f69:超大订单流入占比
    f72:大单净流入金额
    f75:大订单流入占比
    f78:中单净流入金额
    f81:中订单流入占比
    f84:小单净流入金额
    f87:小订单流入占比
    '''
    time_stamp = int(round(time.time() * 1000))
    url_i = 'http://push2.eastmoney.com/api/qt/clist/get?ut=bd1d9ddb04089700cf9c27f6f7426281&' \
            'pi=0&pz=8&po=1&fid=f62&fs=b:' + code + \
            '&=&fields=f1,f2,f12,f13,f14,f62,f152,f128,f3,f62,f184,f66,f69,f72,f75,f78,f81,f84,f87' \
            '&cb=jQuery1124013032222692515671_' + str(time_stamp) + '&_=' + str(time_stamp + 127)
    resp = requests.get(url_i)
    data = eval(str(resp.content.decode('utf-8')).split('(')[1].split(')')[0].replace('null', '""'))['data']
    if data == '':
        return
    total = int(data['total'])
    if total == 0:
        return
    stock_list = data['diff']
    return stock_list


if __name__ == '__main__':
    get_plate_good_stock()
